<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>定时器应用自己写的move函数简单的动画效果可以拿来直接用</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #box1{
            width: 100px;
            height: 100px;
            background-color: #99ff99;
            position: absolute;
            left: 0;
        }
        #box3{
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
            top: 300px;
        }
    </style>
    <script>
        window.onload=function () {
            var box1=document.getElementById("box1");
            var btn01=document.getElementById("btn01");
            var btn02=document.getElementById("btn02");
            var btn03=document.getElementById("btn03");
            btn01.onclick=function (event) {
             move(box1,900,10);
            };
            btn02.onclick=function () {
                move(box1,0,10);
            };
            btn03.onclick=function () {
                move(box3,900,10);
            };
        };
//创建一个函数可以执行简单动画
        /* 参数  1.obj 要执行的对象
        *       2.target 目标
        *       3.speed  移动的速度（正值 向右移动 ;负值 向左移动）
        *
        * */

        function move(obj,target,speed) {
            event=event||window.event;
            //判断speed的正负值 如果从0-800则speed为正 ;如果800-0 则speed为负值
            var current=parseInt(getStyle(obj,"left"));
            if (current>target){
                speed=-speed;
            }
            //点击按钮以后box1向右移动
            clearInterval(obj.timer);
            obj.timer=setInterval(function () {
                var oldValue=parseInt(getStyle(obj,"left"));
                var newValue=oldValue+speed;
                //向右 移动0-800 需要判断newValue是否大于target
                //向左 移动800-0 需要判断newValue是否小于target
                if ((speed>0&&newValue>target) ||(speed<0&&newValue<target)){
                    newValue=target;
                }
               obj.style.left=newValue+"px";
                if (newValue==target){
                    clearInterval(obj.timer);
                }
            },50);
        }
        function getStyle(obj,name){
            // if (window.getComputedStyle){
            //     return getComputedStyle(obj,null)[name];//正常浏览器的方式
            // }else {
            //     return obj.currentStyle[name];//IE
            // }
            return window.getComputedStyle?getComputedStyle(obj,null)[name]:obj.currentStyle[name];

        }
    </script>
</head>
<body>
<button id="btn01">点击按钮以后box1向右移动</button>
<button id="btn02">点击按钮以后box1向左移动</button>
<button id="btn03">点击按钮以后box2向右移动</button>
<br /><br />
<div id="box1"></div>
<div id="box2" style="width: 0;height: 1000px;border-left: 1px black solid;position:absolute;left: 900px;top: 0"></div>
<div id="box3"></div>
</body>
</html>